#include <math.h>

#include <SDL/SDL.h>
#include <SDL_image.h>
#include "gamestate.h"
#include "gameengine.h"
#include "introstate.h"
#include "playstate.h"
#include "menustate.h"

CPlayState playState;
extern CIntroState introState;
extern CMenuState menuState;
void CPlayState::Init()
{
	FILE *mp = fopen("map.txt", "rt");
	int i, j;
	fscanf(mp, "%d", &(mn));

	for(int k=0; k<mn; k++)
	{
		printf("mn = %d k=%d ", mn, k);
		fscanf(mp, "%d", &(msx[k]));
		fscanf(mp, "%d", &(msy[k]));
		printf("msx = %d\nmsy = %d\n",msx[k], msy[k]);

		for(i=0; i<msy[k]; i++)
		{
			for(j=0; j<msx[k]; j++)
			{
				maprend[k][i][j].h = Y;
				maprend[k][i][j].w = X;
				fscanf(mp, "%d ", &(map[k][i][j]));
				if(map[k][i][j]=='#')
				{
					i--;
					break;
				}
				maprend[k][i][j].x=(((map[k][i][j])-1)%msx[k])*X;
				maprend[k][i][j].y=(((map[k][i][j])-1)/msx[k])*Y;
			}
		}
		for(i=0; i<msy[k]; i++)
		{
			for(j=0; j<msx[k]; j++)
			{
				printf("%d ", (map[k][i][j]));
			}
			printf("\n");
		}
	}
	tile = IMG_Load("tile.png");
	bg = IMG_Load("bg.png");
	player = IMG_Load("sk_.png");
	if(!bg||!tile||!player)
	{
		exit(123);
	}
	dstrect.h=player->h;
	dstrect.w=player->w;
	mx = 6*X;
	my = 6*Y;
	mnn=0;
}

void CPlayState::Cleanup()
{
	SDL_FreeSurface(player);
	SDL_FreeSurface(bg);
	SDL_ShowCursor(false);
}

void CPlayState::Pause()
{
}

void CPlayState::Resume()
{
}

void CPlayState::HandleEvents(CGameEngine* game)
{
	SDL_Event event;
	if (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
		case SDL_QUIT:
			game->Quit();
			break;

		case SDL_KEYDOWN:
			switch (event.key.keysym.sym)
			{
			case SDLK_RETURN:
				printf("to menu\n");
				game->PushState( &menuState );
				break;
			case SDLK_ESCAPE:
				printf("to intro\n");
				game->ChangeState( &introState );
				break;
			case SDLK_UP:
				py -=Y;
				break;
			case SDLK_DOWN:
				py +=Y;
				break;
			case SDLK_LEFT:
				px -=X;
				break;
			case SDLK_RIGHT:
				px +=X;
				break;

			}
			break;

		case SDL_KEYUP:
			switch (event.key.keysym.sym)
			{
			case SDLK_UP:
				py =0;
				break;
			case SDLK_DOWN:
				py =0;
				break;
			case SDLK_LEFT:
				px =0;
				break;
			case SDLK_RIGHT:
				px =0;
				break;

			}
			break;

		case SDL_MOUSEMOTION:
			break;
		case SDL_MOUSEBUTTONDOWN:
			switch(event.button.button)
			{
			case SDL_BUTTON_WHEELUP:
				break;
			case SDL_BUTTON_WHEELDOWN:
				break;
			}

		}
	}
}

void CPlayState::Update(CGameEngine* game)
{
	SDL_Delay(100);
	if(mnn==1)
	{
		printf("calc=%d\n",map[mnn][(my+py)/Y][(mx+px)/X]);
		printf("X=%d Y=%d\n",(my+py/X), (mx+px)/Y);
	}
	if(map[mnn][(my+py)/Y][(mx+px)/X]/10==GOTO)
	{
		mnn=map[mnn][(my+py)/Y][(mx+px)/X]%10;
		mx=0;
		my=0;
		ppx=0;
		ppy=0;
		px=0;
		py=0;
		printf("mnn = %d\n", mnn);
	}
	else if(map[mnn][(my+py)/X][(mx+px)/Y]/10!=WALL)
	{
		if(mx+px>=0&&mx+px<=msx[mnn]*X-X&&my+py>=0&&my+py<=msy[mnn]*Y-Y)
		{
			mx = px+mx;
			ppx = px+ppx;
			my = py+my;
			ppy = py+ppy;
		}
	}
	dstrect.x=ppx;
	dstrect.y=ppy;
}

void CPlayState::Draw(CGameEngine* game)
{
	SDL_BlitSurface(bg, NULL, game->screen, NULL);
	SDL_Rect rend;
	rend.h=30;
	rend.w=30;
	for(int i=0; i<msy[mnn]; i++)
	{
		for(int j=0; j<msx[mnn]; j++)
		{
			rend.x=j*X-((mx/X)-5)*X;
			ppx=5*X;
			rend.y=i*Y-((my/Y)-5)*Y;
			ppy=5*Y;
			dstrect.x=ppx;
			dstrect.y=ppy;
			SDL_BlitSurface(tile, &(maprend[mnn][i][j]), game->screen, &rend);
		}
		//SDL_Delay(10000);
	}

	SDL_BlitSurface(player, NULL, game->screen, &dstrect);
}

